<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 1.模拟HTML表单，相当于将HTML表单映射成表单对象，自动将表单对象中的数据拼接成请求参数格式 
         2.异步上传二进制文件 如：图片文件、视频文件-->
         <form id="form">
             <input type="text" name="username">
             <input type="password" name="password">
             <input type="button" id="btn" value="提交">
         </form>
         <script>
             var form = document.getElementById('form');
             var btn = document.getElementById('btn'); 
             btn.onclick = function(){
                //  将普通的HTML表单转换为HTMl表单对象
                 var formData = new FormData(form);
                 //创建ajax对象
                 var xhr = new XMLHttpRequest();
                 xhr.open('post','http://localhost:3000/formData');
                 xhr.send(formData);
                 xhr.onload = function(){
                     if(xhr.status == 200){
                         console.log(xhr.responseText);
                     }
                 }
             }
         </script>
</body>
</html>